var app = getApp();
var baseUrl = app.globalData.host;
Component({
  data: {
    formData: {
    },
    sexCodes: ["请选择","未知的性别", "男性","女性","未说明性别"],
    sexCode: 0,
    invstgSitus: ["请选择","电话调查","现场调查","其他"],
    invstgSituDesc:0,
    showInput:false,
    basInfoBetrues:["请选择","是","否"],
    basInfoBetrueFlag:0,
  },
  methods: {
  formInputChange(e) {
    const {field} = e.currentTarget.dataset;
    this.setData({
      [`formData.${field}`]: e.detail.value,
    })
  },
  bindinputassGuaAbiAssAna: function(e){
    this.setData({
        [`formData.assGuaAbiAssAnaDesc`]:e.detail.value
    }) 
  },
  bindinputassurerModeSitu:function(e){
    this.setData({
      [`formData.assurerModeSituDesc`]:e.detail.value
    }) 
  },
  bindBusinessSexCode: function(e) {
    console.log('picker country 发生选择改变，携带值为', e.detail.value);
    this.setData({
      sexCode: e.detail.value,
      [`formData.sexCode`]: e.detail.value,
    })
  },
  bindBusinesInvstgSitu:function(e){
    console.log('picker country 发生选择改变，携带值为', e.detail.value);
    let showInput = false;
    if(e.detail.value === '3')
    {
      showInput = true;
    }
    this.setData({
      invstgSituDesc: e.detail.value,
      [`formData.invstgSituDesc`]: e.detail.value,
      showInput:showInput
    })
  },
  bindBusinesbBsInfo:function(e){
    console.log('picker country 发生选择改变，携带值为', e.detail.value);
    this.setData({
      basInfoBetrueFlag: e.detail.value,
      [`formData.basInfoBetrueFlag`]: e.detail.value,
    })
  },
    submitForm: function() {
      var data = this.data.formData;
      console.info(data);
      // this.setData({
      //     disabled:true
      // });
      wx.request({
        url: baseUrl+"/report/addRptFarmGuaranteeBusiInfoList",
        method: 'POST',
        dataType: 'json',
        data: data,
        header: {
          'content-type': 'application/json'
        },
        success: function(res) { 
          wx.showToast({
            title: "保存成功",
            icon: 'none',
            duration: 2000,
            success:function(){
              setTimeout(function () {
                //要延时执行的代码
                wx.navigateBack({ //跳转到前一个页面
                  delta:1,//前一个页面
                });
              }, 2000) //延迟时间
            }
          });
          },
          fail: function(res) {
              wx.showToast({
              title: '服务器网络错误,请稍后重试',
              icon: 'loading',
              duration: 1500
              })
          },
          complete: function(res) {
          },
      })    
    },

    onLoad: function(option) {
      var that = this;
      that.setData({
        custId:option.custId,
        [`formData.custId`]: option.custId
      });
      var data = that.data.formData;
      wx.request({
        url: baseUrl+"/report/selectRptFarmGuaranteeBusiInfoList",
        method: 'POST',
        data: data,
        header: {
            'content-type': 'application/json'
        },
        dataType: 'json',
        success: function(res) { 
            console.info(res);
            if(res.data.code == 2)
            {
              wx.showToast({
                title: res.data.data,
                icon: 'none',
                duration: 2000,
                success:function(){
                  setTimeout(function () {
                    //要延时执行的代码
                    wx.navigateBack({ //跳转到前一个页面
                      delta:1,//前一个页面
                    });
                  }, 2000) //延迟时间
                }
              });
            }
            else
            {
              let date = res.data.data
              let sexCode = 0;
              let invstgSituDesc = 0;
              let basInfoBetrueFlag = 0;
              let showInput = false;
              if(date.invstgSituDesc !== null)
              {
                if(date.invstgSituDesc === '3')
                {
                  showInput = true;
                }
                invstgSituDesc = date.invstgSituDesc
              }
              if(date.sexCode !== null)
              {
                sexCode = date.sexCode
              }
              if(date.basInfoBetrueFlag !== null)
              {
                basInfoBetrueFlag = date.basInfoBetrueFlag
              }
              that.setData({
                formData: res.data.data,
                invstgSituDesc:invstgSituDesc,
                sexCode:sexCode,
                basInfoBetrueFlag:basInfoBetrueFlag,
                showInput:showInput,
              })
            }
        },
        fail: function(res) {
            wx.showToast({
            title: '服务器网络错误,请稍后重试',
            icon: 'loading',
            duration: 1500
            })
        },
        complete: function(res) {
        },
      })
   
    }
  }
});